/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author 4pril
 */
public abstract class MyQuery {
    
    protected abstract Class getPOJOClass();
    
    /*
     * Thuc thi cau truy van va tra ra List
     * 
     * @param string hql
     * @return List<Object>
     */
    protected static List<Object> getList(String hql) throws HibernateException{
        
        //mo ket noi
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        //tao cau truy van
        org.hibernate.Query query = session.createQuery(hql);
        
        //thuc thi cau truy van
        List<Object> ds = query.list();
        
        session.close();
        

        return ds;
    }
    
    /*
     * Thuc thi cau truy van va tra ra doi tuong
     * 
     * @param Class c
     *        int id
     * @return Object obj
     */
    protected static Object getObject(Class c, int id){
         //mo ket noi
        Session session = HibernateUtil.getSessionFactory().openSession();

        //thuc thi cau truy van
        Object obj = session.get(c, id);
        
        session.close();
        
        return obj;
    }
    
    /*
     * Them 1 doi tuong vao csdl
     * 
     * @params Object obj
     * @return boolean result
     */
    protected static boolean addObject(Object obj) throws HibernateException{
        
        //mo ket noi csdl
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        //tao transaction
        Transaction tran = null;
        
//        try{
            tran = session.beginTransaction();
            session.save(obj);
            tran.commit();
//        }catch(HibernateException ex){
//            tran.rollback();
//            session.close();
//            return false;
//        }
        
        session.close();
        return true;
    }

    /*
     * Xoa 1 doi tuong khoi csdl
     * thuc te la update Deleted = 1
     */
    protected static boolean removeObject(Object obj) throws HibernateException{
        
        //mo ket noi csdl
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        //tao transaction
        Transaction tran = null;
        
//        try{
            tran = session.beginTransaction();
            session.update(obj);
            tran.commit();
//        }catch(HibernateException ex){
//            tran.rollback();
//            session.close();
//            return false;
//        }
        
        session.close();
        return true;
    }
    
    /*
     * Xoa 1 doi tuong khoi csdl
     * thuc te la update Deleted = 1
     */
    protected static boolean removeObject(Object obj, boolean realDelete) throws HibernateException{
        
        if(realDelete == true){
            //mo ket noi csdl
            Session session = HibernateUtil.getSessionFactory().openSession();

            //tao transaction
            Transaction tran = null;

//            try{
                tran = session.beginTransaction();
                session.delete(obj);
                tran.commit();
//            }catch(HibernateException ex){
//                tran.rollback();
//                session.close();
//                return false;
//            }

            session.close();
            return true;
        }
        
        return false;
    }
    
    /*
     * Update obj
     * 
     * @params Object obj
     * @return boolean ketqua
     */
    protected static boolean updateObject(Object obj) throws HibernateException{
        
        //mo ket noi csdl
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        //tao transaction
        Transaction tran = null;
        
//        try{
            tran = session.beginTransaction();
            session.update(obj);
            tran.commit();
//        }catch(HibernateException ex){
//            tran.rollback();
//            session.close();
//            return false;
//        }
        
        session.close();
        return true;
    }
   
}
